package com.base.lib.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;

import com.base.lib.MyApplication;

import java.util.Map;
import java.util.Set;

public class SpHelper {


    public static SpHelper spHelper;
    public SharedPreferences mSharedPreferences;


    public SpHelper() {
        this(MyApplication.getAppContext(), Context.MODE_PRIVATE);
    }

    public SpHelper(Context context, int mode) {
        mSharedPreferences = context.getSharedPreferences(Constant.PROJECT_NAME, mode);
    }

    public static synchronized SpHelper getInstance() {
        if (spHelper == null) {
            spHelper = new SpHelper();
        }
        return spHelper;
    }

    //string
    public boolean putString(String key, String value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public String getString(String key) {
        return mSharedPreferences.getString(key, "");
    }

    public boolean removeString(String key) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.remove(key);
        return editor.commit();
    }

    //boolean
    public boolean putBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    public boolean getBoolean(String key) {
        return mSharedPreferences.getBoolean(key, true);
    }

    //stringset
    public boolean putStringSet(String key, Set<String> value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putStringSet(key, value);
        return editor.commit();
    }

    @Nullable
    public Set<String> getStringSet(String key) {
        return mSharedPreferences.getStringSet(key, null);
    }

    //int
    public boolean putInt(String key, int value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public int getInt(String key) {
        return mSharedPreferences.getInt(key, -1);
    }

    //long
    public boolean putLong(String key, long value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    public long getLong(String key) {
        return mSharedPreferences.getLong(key, -1);
    }
    // LIST<BEAN>
    /*public boolean setListltruleEunuch(String datalist) {
        return putString(LIST_RULE_EUNUCH, datalist);
    }
    @Nullable
    public ArrayList<LtRuleBean> getListltruleEunuch() {
        return new Gson().fromJson(getString(LIST_RULE_EUNUCH),
                new TypeToken<List<LtRuleBean>>() {
                }.getType());
    }*/


    public void saveAll(Map<String, Object> map) {
        putString(Constant.TOKEN, map.get("token") + ",android");
        putString(Constant.UID, (String) map.get("userid"));
    }

    public String getToken() {
        return getString(Constant.TOKEN);
    }

    public String getUid() {
        return getString(Constant.UID);
    }

    public void clearAll() {
        putString(Constant.TOKEN, "");
        putString(Constant.UID, "");
        putString(Constant.USERNAME, "");
        putString(Constant.NAME, "");
        putString(Constant.DEPTNAME, "");
        putString(Constant.MOBILE, "");
        putString(Constant.SEX, "");

    }

    public void saveUser(Map<String, Object> map) {
        putString(Constant.USERNAME, (String) map.get("username"));
        putString(Constant.NAME, (String) map.get("name"));
        putString(Constant.POSTID, (String) map.get("postId"));
        putString(Constant.DEPTNAME, (String) map.get("deptName"));
        putString(Constant.MOBILE, (String) map.get("mobile"));
        putString(Constant.SEX, (String) map.get("sex"));


    }

    public String getUserName() {
        return getString(Constant.USERNAME);
    }

    public String getName() {
        return getString(Constant.NAME);
    }

    public String getDeptName() {
        return getString(Constant.DEPTNAME);
    }

    public String getMobile() {
        return getString(Constant.MOBILE);
    }

    public String getSex() {
        return getString(Constant.SEX);
    }


}
